<?php
namespace App\Model;

use App\Repository\Sys\Db;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use Illuminate\Support\Facades\Log;

class TeamTable
{
    use Db;
    static private $table = 'team';

    //获取团队信息
    static public function getTeamInfo($id)
    {

        $res = self::ORM()->where('id', $id)->first();
        //$res->img=Str::getImg($res->img);
        //$res->create_time = date('Y-m-d', $res->create_time);

        return $res;
    }


    static public function getMyTeamList($uid)
    {
        $res = \DB::select("select * from team_member LEFT JOIN  team ON  team.id=team_member.team_id WHERE team.status=1 and uid={$uid}", []);
        $data = [];
        foreach ($res as $v) {
            if ($v->uid == $v->admin_id) {
                $v->is_admin = 1;
            } else {
                $v->is_admin = 0;
            }
            $data[] = $v;
        }
        $res = [
            'count' => -1,
            'list' => $data,
            'page' => 1,

        ];
        return JsonRespose::jsonArray(1, $res);

    }


    static public function TeamList($page, $limit, $name, $phone, $is_admin = false)
    {
        if ($is_admin) {
            $where = '1=1';
        } else {
            $where = 'team.status =1';
        }

        $param = [];
        if ($name) {
            $where .= ' and name like :name';
            $param['name'] = "%{$name}%";
        }
        if ($phone) {
            $where .= ' and (phone = :phone or `user`.nickname= :nickname)';
            $param['phone'] = $phone;
            $param['nickname'] = $phone;
        }
        $count = \DB::select("select count(*) as c  from team LEFT  JOIN `user` on user.id=team.admin_id WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select team.*,user.phone from team LEFT JOIN `user`  on `user`.id= team.admin_id WHERE {$where} ORDER BY team.id DESC limit :offset,:limit", $param);


        $data = [];
        foreach ($res as $v) {
            $v->create_time = date('Y-m-d', $v->create_time);
            $v->admin_name = UserTable::getUserName($v->admin_id);
            $v->count = \DB::select("select count(*) as c  from invite_team WHERE  status=3 and (team_id={$v->id} or join_team_id={$v->id})", [])[0]->c;  //完成比赛次数
            $data[] = $v;
        }
        $res = [
            'count' => $count,
            'list' => $data,
            'page' => $page,

        ];
        return JsonRespose::jsonArray(1, $res);
    }

    static public function getWin($team_id)
    {
       
        $count_all = \DB::select("select count(*) as c  from invite_team WHERE  status>=2 and team_id={$team_id}", [])[0]->c;  //总
        $count_ok = \DB::select("select count(*) as c  from invite_team WHERE  status=3 and team_id={$team_id}", [])[0]->c;  //完成比赛次数

//        return "$count_all ,   $count_ok";
        if (!$count_all) {
            return 100;
        }
        if ($count_all and !$count_ok) {
            return 0;
        }
        $win = (int)(($count_ok / $count_all) * 100);
        if ($win > 100) {
            Log::error("靠谱率异常  all {$count_all} : {$count_ok}");
            $win = 0;
        }


        return $win;

    }

    //更新团队人数
    static public function updateCountNember($team_id)
    {
        $res = TeamMemberTable::ORM()->where('team_id', $team_id)->where('status', 1)->get();
        $count = 0;
        $avg = 0;
        $avg_c = 0;
        foreach ($res as $v) {
            $count += 1;
            $age = UserTable::getUserInfo($v->uid)['data']->age;
            if ($age) {
                $avg += $age;
                $avg_c += 1;
            }
        }
        if ($avg) {
            $avg = (int)($avg / $avg_c);
        }

        TeamTable::ORM()->where('id', $team_id)->update(['people_number' => $count]);  //'avg_age' => $avg  年龄好像不需要

    }


}